package com.thinta.wind.ui.login

import android.content.Intent
import android.text.Editable
import android.text.TextWatcher
import android.view.View
import android.widget.Toast
import androidx.core.content.ContextCompat
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import com.thinta.wind.JetPackDMApp
import com.thinta.wind.R
import com.thinta.wind.base.BaseActivity
import com.thinta.wind.databinding.ActivityLoginBinding
import com.thinta.wind.ui.main.MainActivity
import com.thinta.wind.utils.PreferenceUtils
import kotlinx.android.synthetic.main.activity_login.*

class LoginActivity : BaseActivity<LoginViewModel, ActivityLoginBinding>() {

    var account = "test1"
    var pwd = "123"
    var accEmptyFlag = true
    var pwdEmptyFlag = true

    override fun getModelFactory(): ViewModelProvider.NewInstanceFactory = LoginModelFactory(LoginRepository())

    override fun viewModelClass(): Class<LoginViewModel> = LoginViewModel::class.java

    override fun startListenerData() {
        dataBinding?.account?.addTextChangedListener(object : TextWatcher{
            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {}

            override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {}

            override fun afterTextChanged(p0: Editable?) {
                if(p0!!.isNotEmpty()){
                    account = p0.toString()
                    accEmptyFlag = false
                    account_icon.visibility = View.VISIBLE
                    if(!pwdEmptyFlag) login.setBackgroundColor(ContextCompat.getColor(this@LoginActivity, R.color.color_orange_2))
                }else{
                    accEmptyFlag = true
                    account_icon.visibility = View.GONE
                    login.setBackgroundColor(ContextCompat.getColor(this@LoginActivity, R.color.color_orange_1))
                }
            }
        })
        dataBinding?.pwd?.addTextChangedListener(object : TextWatcher{
            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {}

            override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {}

            override fun afterTextChanged(p0: Editable?) {
                if(p0!!.isNotEmpty()){
                    pwd = p0.toString()
                    pwdEmptyFlag = false
                    pwd_icon.visibility = View.VISIBLE
                    if(!accEmptyFlag) login.setBackgroundColor(ContextCompat.getColor(this@LoginActivity, R.color.color_orange_2))
                }else{
                    pwdEmptyFlag = true
                    pwd_icon.visibility = View.GONE
                    login.setBackgroundColor(ContextCompat.getColor(this@LoginActivity, R.color.color_orange_1))
                }
            }
        })
        account_icon.setOnClickListener {
            dataBinding?.account?.text?.clear()
        }
        pwd_icon.setOnClickListener {
            dataBinding?.pwd?.text?.clear()
        }
        login.setOnClickListener {
            viewModel?.login(account, pwd)
        }
        viewModel?.userData?.observe(this, Observer{
            if (it == null){
                Toast.makeText(this, "登录失败", Toast.LENGTH_SHORT).show()
            }else{
                Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show()
                PreferenceUtils.setObject(PreferenceUtils.USER_INFO, it)
                startActivity(Intent(this@LoginActivity, MainActivity::class.java))
            }
        })
    }

    override fun initView() {
        dataBinding?.accountText = account
        dataBinding?.pwdText = pwd
        startListenerData()
    }

    override fun getLayoutRes() : Int = R.layout.activity_login

}
